package com.handu.luna.core.domain.goods;

import com.handu.luna.core.domain.BasePersistentDomain;
import com.handu.luna.core.domain.promotion.*;

import org.apache.commons.collections.MapUtils;
import org.springframework.data.repository.NoRepositoryBean;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by wangzailiang on 2014/8/28.
 */
@SuppressWarnings("rawtypes")
@NoRepositoryBean
public class GoodsInfo extends BasePersistentDomain {

    public enum UltimatePriceType {
        SECKILL("秒杀"),GROUPBUY("团购"),SALESPROMOTION("促销活动"),USERRANK("用户级别"),SHOP("本店价格");

        public String description;
        UltimatePriceType(String description) {
            this.description = description;
        }
    }

    private Goods goods;
    //最终价格类型
    private UltimatePriceType ultimatePriceType;
    //最终价格
    private BigDecimal ultimatePrice;

    private Map<String,Promotion> promotionMap;
    private Map<String,BigDecimal> priceMap;
    /*//是否秒杀
    private Boolean isSecKill;
    //是否团购
    private Boolean isGroupBuy;
    //是否促销活动
    private Boolean isSalesPromotion;
    //是否优惠活动
    private Boolean isActivities;*/





    public GoodsInfo() {

    }

    public void init(long id) {
        goods = context.getGoodsRepository().findGoodById(id);
        //initSeckillGroupBuy(id);
        //initSalesPromotion(id);
        initUltimatePrice();
    }

    public Map<String,Promotion> initPromotionMap() {
        if(promotionMap == null) {
            promotionMap = new HashMap<>();
        }
        return promotionMap;

    }

     public Map<String,BigDecimal> initPriceMap() {
        if(priceMap == null) {
            priceMap = new HashMap<>();
        }
        return priceMap;

    }

    public void initUltimatePrice() {

        if(MapUtils.isNotEmpty(this.priceMap)) {
            String ultimateType = "";
            for(Map.Entry<String,BigDecimal> entry: priceMap.entrySet()) {
                if(ultimatePrice == null) {
                    ultimatePrice = this.goods.getShopPrice();
                }
                if(entry.getValue().compareTo(ultimatePrice) == -1) {
                    ultimatePrice = entry.getValue();
                    ultimateType = entry.getKey();
                }
            }
            initUltimateType(ultimateType);
        }
    }

    public void initUltimateType(String ultimateType) {

        switch(ultimateType) {
            case "团购":
                this.ultimatePriceType = UltimatePriceType.GROUPBUY;
                break;
            case "秒杀":
                this.ultimatePriceType = UltimatePriceType.SECKILL;
                break;
            case "促销活动":
                this.ultimatePriceType = UltimatePriceType.SALESPROMOTION;
                break;
            default:
                this.ultimatePriceType = UltimatePriceType.SHOP;
                break;
        }

    }

    /**
     * 查看此商品是否有团购秒杀
     * @param goodsId
     * @return
     */
  /*  public Map<String,Promotion> initSeckillGroupBuy(long goodsId) {

        SecKillGroupBuy secKillGroupBuy = context.getSecKillGroupBuyGoodsRepository().findSecKillGroupBuyByGoodsId(goodsId,new Date());
        if(secKillGroupBuy != null) {

            initPromotionMap().put(secKillGroupBuy.getSecKillGroupBuyStatus().description,secKillGroupBuy);

            SecKillGroupBuyGoods secKillGroupBuyGoods = context.getSecKillGroupBuyGoodsRepository().findBySecKillGroupBuyIdAndGoodsId(secKillGroupBuy.getId(), goodsId);

            initPriceMap().put(secKillGroupBuy.getSecKillGroupBuyStatus().description,secKillGroupBuyGoods.getGoodsPrice());

        }
        return promotionMap;
    }*/

    /**
     * 查看此商品是否有促销活动
     * @param goodsId
     * @return
     */
//    public Map<String,Promotion> initSalesPromotion(long goodsId) {
//
//        SalesPromotion salesPromotion = context.getSalesPromotionGoodsRepository().findSalesPromotionByGoodsId(goodsId,new Date());
//        if(salesPromotion != null) {
//            initPromotionMap().put(salesPromotion.getType().description,salesPromotion);
//
//            SalesPromotionGoods salesPromotionGoods = context.getSalesPromotionGoodsRepository().findBySalesPromotionIdAndGoodsId(salesPromotion.getId(),goodsId);
//
//            initPriceMap().put(salesPromotion.getType().description,salesPromotionGoods.getGoodsPrice());
//        }
//        return promotionMap;
//    }

    public Goods getGoods() {
        return goods;
    }

    public void setGoods(Goods goods) {
        this.goods = goods;
    }

    public UltimatePriceType getUltimatePriceType() {
        return ultimatePriceType;
    }

    public void setUltimatePriceType(UltimatePriceType ultimatePriceType) {
        this.ultimatePriceType = ultimatePriceType;
    }

    public BigDecimal getUltimatePrice() {
        return ultimatePrice;
    }

    public void setUltimatePrice(BigDecimal ultimatePrice) {
        this.ultimatePrice = ultimatePrice;
    }

    public Map<String, Promotion> getPromotionMap() {
        return promotionMap;
    }

    public void setPromotionMap(Map<String, Promotion> promotionMap) {
        this.promotionMap = promotionMap;
    }

    public Map<String, BigDecimal> getPriceMap() {
        return priceMap;
    }

    public void setPriceMap(Map<String, BigDecimal> priceMap) {
        this.priceMap = priceMap;
    }


    /**
     * 查看当前用户的会员等级
     */






}
